<template>
  <div class="login">
    <div
      class="login-card animate__animated animate__backInDown"
      ref="loginCard"
    >
      <div class="login-img" ref="loginImg"></div>
      <div class="login-right" ref="loginRight">
        <div class="from">
          <div class="head">
            <div class="title" ref="title">智能停车场管理平台</div>
            <div class="title-detail" ref="titleDetail">
              请使用您本人的账号密码登录
            </div>
          </div>
          <div class="body">
            <div class="input-group">
              <div class="icon">
                <i class="iconfont icon-24gf-lock"></i>
              </div>
              <div>
                <h5>Username</h5>
                <input
                  type="text"
                  class="input"
                  v-model="info.username"
                  @blur="blurFunction($event)"
                  @focus="focusFunction($event)"
                />
              </div>
            </div>
            <div class="input-group">
              <div class="icon"><i class="iconfont icon-yonghu"></i></div>
              <div>
                <h5>Password</h5>
                <input
                  type="password"
                  class="input"
                  v-model="info.password"
                  @blur="blurFunction($event)"
                  @focus="focusFunction($event)"
                />
              </div>
            </div>
            <div class="input-group">
              <div class="icon">
                <i class="iconfont icon-yanzhengma"></i>
              </div>
              <div>
                <h5>Captcha</h5>
                <input
                  type="text"
                  class="input2"
                  v-model="info.code"
                  @blur="blurFunction($event)"
                  @focus="focusFunction($event)"
                  @keydown.enter="toLogin"
                />
                <span @click="refresh" style="cursor: pointer"
                  ><img :src="vercodeImg"
                /></span>
              </div>
            </div>
          </div>
          <div class="submit">
            <button @click="toLogin">登录</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Qs from "qs";
export default {
  name: "Login",
  components: {},
  data() {
    return {
      info: {
        username: "",
        password: "",
        code: "",
        id: "",
      },

      vercodeImg: "",
    };
  },
  created() {
    this.getVercode();
  },
  mounted() {},
  methods: {
    //获取验证码
    getVercode() {
      this.axios
        .get("/user/getCaptchaBase64")
        .then((res) => {
          this.info.id = res.data.id;
          this.vercodeImg = res.data.url;
        })
        .catch((e) => {
          this.$message.error("获取验证码失败，请重试");
          console.log(e);
        });
    },
    //刷新验证码
    refresh() {
      this.getVercode();
    },
    focusFunction(e) {
      const el = e.target;
      let parentNode = el.parentNode.parentNode;
      parentNode.classList.add("focus");
    },
    blurFunction(e) {
      const el = e.target;
      let parentNode = el.parentNode.parentNode;
      if (el.value == "") {
        parentNode.classList.remove("focus");
      }
    },
    //登录
    toLogin() {
      this.$store
        .dispatch("Login", Qs.stringify(this.info))
        .then((res) => {
          const msg = res.data.msg;
          if (res.data.code === "401" || res.data.code === "500") {
            this.$message.error(msg);
            this.refresh();
          } else if (res.data.code === "400") {
            this.$message({
              message: msg,
              type: "warning",
            });
            this.refresh();
          } else {
            this.$message({
              message: msg,
              type: "success",
            });
            this.$router.push("/index");
          }
        })
        .catch((err) => {
          this.$message.error(err);
          this.refresh();
        });
    },
  },
};
</script>

<style scoped>
@font-face {
  font-family: RS_Xi;
  src: url("../assets/font/RS_Xi.ttf"), url("../assets/font/RS_Xi.ttf");
}

.login {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 1383px;
  min-height: 718px;
  margin: 0;
  background-image: linear-gradient(#ffbe79, #ffbe79 10%, #75bed6 40%, #75bed6);
  overflow: hidden;
  height: 100vh;
}
.login-card {
  position: absolute;
  top: 20%;
  left: 18%;
  background-image: url("../assets/image/title-img.png");
  background-size: 100% 100%;
  width: 65vw;
  min-width: 911px;
  height: 65vh;
  background-color: #ffffff;
  border-radius: 0 15px 15px 0;
}
.login-card-night {
  background-color: #5f27cd;
}
.login-card:hover {
  box-shadow: 0px 0px 35px #a3bffe;
}
.login-img {
  float: left;
  width: 50%;
  height: 100%;
  background-image: url("../assets/image/login-left.png");
  background-size: 100% 100%;
}
.login-img-night {
  background: rgba(0, 0, 0, 0.4);
}
.login-right {
  position: relative;
  float: right;
  width: 50%;
  height: 100%;
}
.login-right-night {
  background-color: #200f44;
}
.login-right .head {
  height: 25%;
}
.login-right .title {
  float: left;
  width: 90%;
  min-width: 295px;
  height: 8vh;
  line-height: 8vh;
  color: #5794ff;
  font-size: 2rem;
  font-family: RS_Xi;
}

.text-night {
  color: #f6e58d;
  text-shadow: 0 0 5px #a3bffe;
}

.login-right .title-detail {
  float: left;
  width: 80%;
  min-width: 262px;
  height: 5vh;
  line-height: 5vh;
  color: #909399;
}

.login-right .from {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 1.5rem 4rem;
  text-align: left;
}
.input-group {
  position: relative;
  display: grid;
  grid-template-columns: 7% 93%;
  margin: 18px 0;
  padding: 5px 0;
  border-bottom: 2px solid #d9d9d9;
}

.input-group:nth-child(1) {
  margin-bottom: 4px;
}
.input-group:before,
.input-group:after {
  content: "";
  position: absolute;
  bottom: -2px;
  width: 0;
  height: 2px;
  background-color: #79a9ff;
  transition: 0.5s;
}
.input-group:after {
  right: 50%;
}
.input-group:before {
  left: 50%;
}
.icon {
  display: flex;
  /* justify-content: center; */
  align-items: center;
}
.icon i {
  color: #d9d9d9;
  transition: 0.5s;
}

.input-group > div {
  position: relative;
  height: 45px;
}

.input-group > div > h5 {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #d9d9d9;
  font-size: 15px;
  transition: 0.3s;
}
.input-group.focus .icon i {
  color: #5794ff;
}
.input-group.focus div h5 {
  left: -15px;
  top: -5px;
  font-size: 15px;
  color: #79a9ff;
}
.input-group.focus:after,
.input-group.focus:before {
  width: 50%;
}
.input {
  position: absolute;
  width: 100%;
  height: 100%;
  top: -5px;
  left: 0;
  border: none;
  outline: none;
  background: none;
  padding: 0.5rem 0.7rem;
  font-size: 1.3rem;
  color: #555;
  font-family: "Roboto", sans-serif;
}
.input2 {
  position: absolute;
  width: 40%;
  height: 100%;
  top: -5px;
  left: 0;
  border: none;
  outline: none;
  background: none;
  padding: 0.5rem 0.7rem;
  font-size: 1.3rem;
  color: #555;
  font-family: "Roboto", sans-serif;
}
.input-group img {
  position: absolute;
  width: 35%;
  height: 100%;
  top: -5px;
  right: 20px;
}
.submit {
  position: absolute;
  left: 0;
  bottom: 8vh;
  transform: translate(30%, 0%);
  width: 80%;
}
.submit button {
  width: 55%;
  height: 5vh;
  border: 0;
  border-radius: 18vh;
  background-color: #5794ff;
  font-size: 1rem;
  color: #fff;
  vertical-align: middle;
}
.submit button:hover {
  cursor: pointer;
  background-color: rgb(105, 155, 240);
}
.submit button:active {
  background-color: rgb(61, 126, 238);
}
</style>